package com.zd.bms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.bms.service.IBmsResourceService;
import com.zd.common.bean.model.BmsResource;
import com.zd.common.dao.BmsResourceDao;
import com.zd.common.dao.common.BaseDao;
import com.zd.common.dao.common.FindRequestEntity;
import com.zd.common.dao.common.WhereEnum;
import com.zd.common.service.impl.BaseServiceImpl;

@Service
public class BmsResourceServiceImpl extends BaseServiceImpl<BmsResource> implements IBmsResourceService {
	@Autowired
	private BmsResourceDao bmsResourceDao;

	@Override
	public BaseDao<BmsResource> getBaseDao() {
		return bmsResourceDao;
	}

	public String getFullUriName(String uri) throws Exception {
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addWhere("url", WhereEnum.EQ, uri);
		List<BmsResource> bmsResourceList = find(findReq);

		if (bmsResourceList != null && bmsResourceList.size() > 0) {
			BmsResource bmsResource = bmsResourceList.get(0);
			String fullName = getFullName(bmsResource);
			return fullName;
		}
		return "";
	}

	private String getFullName(BmsResource bmsResource) {
		if (bmsResource == null) {
			return "";
		}
		String name = "";
		if (bmsResource.getParentId() != null && bmsResource.getParentId() != 0) {
			BmsResource parentResource = get(bmsResource.getParentId());
			name = getFullName(parentResource) + " - " + bmsResource.getName();
		} else {
			name = bmsResource.getName();
		}
		return name;
	}
}
